<template>
  <div class="orders">

    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card>

      <!-- 搜索栏 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryData.query" clearable @clear="getOrders">
            <el-button slot="append" icon="el-icon-search" @click="getOrders"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <el-row>

        <!-- Tabel区域 -->
        <template>
          <el-table :data="tableData" style="width: 100%" border stripe>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column prop="order_number" label="订单编号"></el-table-column>
            <el-table-column prop="order_price" label="订单价格"></el-table-column>
            <el-table-column prop="order_pay" label="是否付款">
              <template v-slot="scoped">
                <el-tag type="danger" v-if="scoped.order_pay === 0">未付款</el-tag>
                <el-tag type="success" v-else>已付款</el-tag>
              </template>
            </el-table-column>

            <el-table-column prop="is_send" label="是否发货"></el-table-column>

            <el-table-column prop="create_time" label="下单时间"></el-table-column>

            <el-table-column label="操作">
              <el-button type="primary" icon="el-icon-edit"></el-button>
              <el-button type="success" icon="el-icon-location-outline"></el-button>
            </el-table-column>
          </el-table>
        </template>
      </el-row>

      <el-row>
        <!-- 分页区域 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryData.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryData.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  components: {},
  props: {},
  data () {
    return {
      queryData: { // 请求Tabel数据参数
        query: null,
        pagenum: 1,
        pagesize: 5,
        user_id: null,
        pay_status: null,
        is_send: null,
        order_fapiao_title: null,
        order_fapiao_company: null,
        order_fapiao_content: null,
        consignee_addr: null
      },
      tableData: [], // Tabel数据
      total: 0
    }
  },
  watch: {},
  computed: {},
  methods: {
    // 获取订单数据
    async getOrders () {
      const { data: res } = await this.$http.get('orders', { params: this.queryData })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      res.data.goods.forEach(item => {
        item.create_time = this.doTime(item.create_time)
        item.update_time = this.doTime(item.update_time)
      })
      this.tableData = res.data.goods
      this.total = res.data.total
    },

    // 处理时间
    doTime (timestamp) {
      const date = new Date(timestamp * 1000) // 时间戳为秒，需要乘以1000转换为毫秒
      const year = date.getFullYear()
      const month = (date.getMonth() + 1).toString().padStart(2, '0')
      const day = date.getDate().toString().padStart(2, '0')
      const hours = date.getHours().toString().padStart(2, '0')
      const minutes = date.getMinutes().toString().padStart(2, '0')
      const seconds = date.getSeconds().toString().padStart(2, '0')
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
    },

    // 分页改变
    handleSizeChange (newVal) {
      this.queryData.pagesize = newVal
      this.getOrders()
    },

    // 页码改变
    handleCurrentChange (newVal) {
      this.queryData.pagenum = newVal
      this.getOrders()
    }
  },
  created () {
    this.getOrders()
  },
  mounted () { }
}
</script>
<style lang="scss" scoped></style>
